﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Bardent.Weapons.Components.ComponentData
{
    /// <summary>
    /// 组件数据
    /// </summary>
    [Serializable]
    public class ComponentData
    {
        [SerializeField,HideInInspector]
        private string name;

        [SerializeField]
        public Type ComponentDependency { get; protected set; }

        public void SetComponentName() => name=GetType().Name;

        public ComponentData()
        {
            //每次构建自动刷新名称
            SetComponentName();
        }

        public virtual void SetAttackDataNames() { }

        public virtual void InitializeAttackData(int numberOfAttack) {}

    }

    [Serializable]
    public class ComponentData<T>:ComponentData where T : AttackDatas
    {
        
        public T[] AttackData { get=>attackData; private set=>attackData=value; }

        [field: SerializeField]
        public T[] attackData;

        public override void SetAttackDataNames()
        {
            base.SetAttackDataNames();
            for (var i = 0; i < AttackData.Length; i++)
            {
                AttackData[i].SetAttackName(i + 1);
            }
        }

        /// <summary>
        /// 攻击次数
        /// </summary>
        /// <param name="numberOfAttack"></param>
        public override void InitializeAttackData(int numberOfAttack)
        {
            base.InitializeAttackData(numberOfAttack);
            var oldLen = attackData!=null?attackData.Length:0;
            if (oldLen==numberOfAttack)
            {
                return;
            }

            Array.Resize(ref attackData, numberOfAttack);

            if (oldLen<numberOfAttack)
            {
                for (var i = oldLen; i < attackData.Length; i++)
                {
                    var newObj = Activator.CreateInstance(typeof(T)) as T;
                    attackData[i] = newObj;
                }
            }

            SetAttackDataNames();
        }

    }
}
